package com.lichongbing.lyoggl.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.lichongbing.lyoggl.model.Account;
import com.lichongbing.lyoggl.service.AccountService;
import com.lichongbing.lyoggl.service.LyogglUsersService;
import com.lichongbing.lyoggl.utils.JwtTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2022/8/10 00:02
 * @description: TODO
 */
@Slf4j
@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private LyogglUsersService lyogglUsersService;

    /**
     * 新增账单
     * @param jsonObject
     * @param request
     * @return
     */
    @PostMapping("/create")
    public String createProject(@RequestBody JSONObject jsonObject, HttpServletRequest request){
        String token = request.getHeader("token");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        Account project = new Account();
        String year = String.valueOf(DateUtil.thisYear());
        int mouth = DateUtil.thisMonth();
        String id = UUID.randomUUID().toString().replace("-", "");

        if(expiration==false){
            String userid = JwtTokenUtils.getUserid(token);

            boolean flag = lyogglUsersService.isToken(userid);
            if(flag){
                project.setId(id);
                project.setBankid(jsonObject.getString("bankid"));
                project.setMoney(jsonObject.getString("money"));
                project.setMoney1(jsonObject.getString("money1"));
                project.setMoney2(jsonObject.getString("money2"));
                project.setMoney3(jsonObject.getString("money3"));
                project.setPay(jsonObject.getString("pay"));
                project.setPay1(jsonObject.getString("pay1"));
                project.setMouth(mouth+1);
                project.setYear(year);
                project.setUid(userid);
                accountService.save(project);
                return "添加成功";
            }else {
                return "token无效";
            }

        }else {
            return "token过期";
        }
    }


    /**
     * 根据用户id查询账单
     * @param request
     * @return
     */

    @PostMapping("/findByUid")
    public JSONObject findByUid(HttpServletRequest request){
        JSONObject jsonObject = new JSONObject();

        String token = request.getHeader("token");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        if (expiration == false) {
            String userid = JwtTokenUtils.getUserid(token);
            boolean flag = lyogglUsersService.isToken(userid);
            if(flag) {
                jsonObject.put("code", 200);
                jsonObject.put("msg", "查询成功");
                jsonObject.put("data", accountService.findByUid(userid));
                return jsonObject;
            }else {
                jsonObject.put("code",402);
                jsonObject.put("msg","token无效");
                return jsonObject;
            }
        }else {
            jsonObject.put("code",400);
            jsonObject.put("msg","token过期");
            return jsonObject;
        }
    }


    /**
     * 根据id删除账单
     * @param id
     * @param request
     * @return
     */
    @PostMapping("/delete/{id}")
    public JSONObject delete(@PathVariable String id, HttpServletRequest request){
        JSONObject jsonObject = new JSONObject();
        String token = request.getHeader("token");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        if(expiration==false){
            String userid = JwtTokenUtils.getUserid(token);
            boolean flag = lyogglUsersService.isToken(userid);
            if(flag){
                accountService.deleteByUid(id,userid);
                jsonObject.put("code",200);
                jsonObject.put("msg","删除成功");
                return jsonObject;
            }else {
                jsonObject.put("code",402);
                jsonObject.put("msg","token无效");
                return jsonObject;
            }
        }else {

            jsonObject.put("code",400);
            jsonObject.put("msg","token过期");
            return jsonObject;
        }

    }




}
